package com.road.yishi.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

import com.road.entity.lottery.LotteryTempInfo;
import com.road.yishi.dao.LotteryTempDao;
import com.road.yishi.db.DbParameter;
import com.road.yishi.db.StrategyBaseDao;

public class LotteryTempDaoImpl extends StrategyBaseDao implements LotteryTempDao {

	public boolean addLotteryTempInfo(LotteryTempInfo info) {
		if (info == null) {
			return false;
		}
		boolean result = false;
		if (info.beginAdd()) {
			String sql = "INSERT INTO `t_s_lottery`(`Uuid`,`Types`,`IsOpen`,`OpenDate`,`StopDate`,`DayMaxCount`,`OneNeedPoint`,`Property1`,`Property2`,`Property3`,`Property4`,`LastOpenDate`) VALUES(?,?,?,?,?,?,?,?,?,?,?,?);";
			Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
			para.put(1, new DbParameter(Types.VARCHAR, info.getUuid()));
			para.put(2, new DbParameter(Types.INTEGER, info.getTypes()));
			para.put(3, new DbParameter(Types.TINYINT, info.getIsOpen()));
			para.put(4, new DbParameter(Types.TIMESTAMP, info.getOpenDate()));
			para.put(5, new DbParameter(Types.TIMESTAMP, info.getStopDate()));
			para.put(6, new DbParameter(Types.INTEGER, info.getDayMaxCount()));
			para.put(7, new DbParameter(Types.INTEGER, info.getOneNeedPoint()));
			para.put(8, new DbParameter(Types.INTEGER, info.getProperty1()));
			para.put(9, new DbParameter(Types.INTEGER, info.getProperty2()));
			para.put(10, new DbParameter(Types.VARCHAR, info.getProperty3()));
			para.put(11, new DbParameter(Types.VARCHAR, info.getProperty4()));
			para.put(12, new DbParameter(Types.TIMESTAMP, info.getLastOpenDate()));
			result = execNoneQuery(sql, para) > -1 ? true : false;
			info.commitAdd(result);
		}
		return result;
	}

	public boolean updateLotteryTempInfo(LotteryTempInfo info) {
		if (info == null) {
			return false;
		}
		boolean result = false;
		if (info.beginUpdate()) {
			String sql = "UPDATE `t_s_lottery` SET `IsOpen`=?,`OpenDate`=?,`StopDate`=?,`DayMaxCount`=?,`OneNeedPoint`=?,`Property1`=?,`Property2`=?,`Property3`=?,`Property4`=?,`LastOpenDate`=? WHERE `Uuid`=?;";
			Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
			para.put(1, new DbParameter(Types.TINYINT, info.getIsOpen()));
			para.put(2, new DbParameter(Types.TIMESTAMP, info.getOpenDate()));
			para.put(3, new DbParameter(Types.TIMESTAMP, info.getStopDate()));
			para.put(4, new DbParameter(Types.INTEGER, info.getDayMaxCount()));
			para.put(5, new DbParameter(Types.INTEGER, info.getOneNeedPoint()));
			para.put(6, new DbParameter(Types.INTEGER, info.getProperty1()));
			para.put(7, new DbParameter(Types.INTEGER, info.getProperty2()));
			para.put(8, new DbParameter(Types.VARCHAR, info.getProperty3()));
			para.put(9, new DbParameter(Types.VARCHAR, info.getProperty4()));
			para.put(10, new DbParameter(Types.TIMESTAMP, info.getLastOpenDate()));
			para.put(11, new DbParameter(Types.VARCHAR, info.getUuid()));
			result = execNoneQuery(sql, para) > -1 ? true : false;
			info.commitUpdate(result);
		}
		return result;
	}

	public LotteryTempInfo getLotteryTempInfo(int types) {
		String sql = "SELECT * FROM `t_s_lottery` WHERE Types=?;";
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		para.put(1, new DbParameter(Types.INTEGER, types));
		PreparedStatement pstmt = execQuery(sql, para);
		ResultSet rs = null;
		LotteryTempInfo info = null;
		if (pstmt != null) {
			try {
				rs = pstmt.executeQuery();
				if (rs.last()) {
					info = new LotteryTempInfo();
					info.setUuid(rs.getString("Uuid"));
					info.setTypes(rs.getInt("Types"));
					info.setIsOpen(rs.getBoolean("IsOpen"));
					info.setOpenDate(rs.getTimestamp("OpenDate"));
					info.setStopDate(rs.getTimestamp("StopDate"));
					info.setDayMaxCount(rs.getInt("DayMaxCount"));
					info.setOneNeedPoint(rs.getInt("OneNeedPoint"));
					info.setProperty1(rs.getInt("Property1"));
					info.setProperty2(rs.getInt("Property2"));
					info.setProperty3(rs.getString("Property3"));
					info.setProperty4(rs.getString("Property4"));
					info.setLastOpenDate(rs.getTimestamp("LastOpenDate"));
				}
			} catch (SQLException e) {
				info = null;
				Log.error("执行出错" + sql, e);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return info;
	}

}